package in.muton.edushare.pages;

import in.muton.edushare.EduSharePage;
import in.muton.edushare.EduShareSession;
import in.muton.edushare.registration.ForgotPassword;
import in.muton.edushare.url.URLLoader;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.RestartResponseException;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;

import com.edushare.common.dao.UsersDAO;
import com.edushare.common.vo.Users;

public class EduShareLogin extends EduSharePage {

	private static final long serialVersionUID = -4949322049550398695L;
	private static Log log = LogFactory.getLog(EduShareLogin.class);
			
	public EduShareLogin() {
		if(EduShareSession.get().getLoggedInUser()!=null){
			//if already logged-in take him to landing page of his.
			log.debug("redirecting to user's home page");
			throw new RestartResponseException(URLLoader.getLandingPageClass(EduShareSession.get().getLoggedInUser().getLandingPageId()));
		}
		add(CSSPackageResource.getHeaderContribution(EduShareLogin.class, "EduShareLogin.css"));
		add(new FeedbackPanel("msg"));
		final TextField<String> username = new TextField<String>("username", new Model<String>(""));
		username.setRequired(true);
		final PasswordTextField password = new PasswordTextField("password", new Model<String>(""));
		password.setRequired(true);
		Form<String> loginForm = new Form<String>("loginform"){			
			private static final long serialVersionUID = 2846077880935569835L;
			@Override
			protected void onSubmit() {
				log.info("Loggin in: " + username.getModelObject());
				/** This should be done using a validator */
				Users user = new UsersDAO(EduShareSession.get()
											.getSessionORMInterface())
											.getAuthenticUser
											(username.getModelObject(), 
											 password.getModelObject());
				if(user!=null){
					EduShareSession.get().logInUser(user);
					if(!continueToOriginalDestination()){
						setResponsePage(URLLoader.getLandingPageClass(user.getLandingPageId()));//Do not modify here, change in getLanding page method
					}
				}else{
					EduShareSession.get().error("Wrong username password combination!");
					setResponsePage(in.muton.edushare.pages.EduShareLogin.class);
				}
			}
			
			@Override
			protected void onError() {
				super.onError();				
			}
		};
		
//		loginForm.add(new AuthenticationValidator(username, password));
		add(loginForm);
		loginForm.add(username);
		loginForm.add(password);
		loginForm.add(new Link<Object>("lostPassword"){
			private static final long serialVersionUID = 8395265778634904976L;
			@Override
			public void onClick() {
				setResponsePage(ForgotPassword.class);
			}
		});
		
		FooterLinks fl = new FooterLinks("footerLinks", CSSPackageResource.getHeaderContribution(EduShareLogin.class, "FooterWithColorLinks.css"));
		add(fl);
	}
	
}
